package com.wr.recruit.service.apis;

import com.wr.recruit.service.apis.dto.UserDto;
import com.wr.recruit.service.dao.dbo.UserDbo;
import com.wr.recruit.service.service.LoginService;
import com.wr.recruit.service.service.UserService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Controller
public class LoginApis {

    private UserService userService;
    private LoginService loginService;

    @Autowired
    public LoginApis(UserService userService,
                     LoginService loginService) {
        this.userService = userService;
        this.loginService = loginService;
    }

    @PostMapping(value = "/login", produces = "application/json")
    @ResponseBody
    public APIResponse login(@RequestBody UserDto userDto){
        UserDbo userInfo = userService.getUserInfo(userDto.getUserName());
        if (Objects.isNull(userDto))
            return new APIResponseSet<>(false, "无此账户");
        if (loginService.login(userInfo.getUserName(),userInfo.getPassword(),userDto.getPassword()))
            return new APIResponseSet<>(false, "登陆失败");
        return new APIResponseSet<>(true, "登陆成功");
    }

}
